﻿using Entitas;
using UnityEngine;

public class LifeTimeSystem : IExecuteSystem
{
    private readonly IGroup<GameEntity> m_Group;

    public LifeTimeSystem (Contexts contexts)
    {
        m_Group = contexts.game.GetGroup (GameMatcher.LifeTimeComp);
    }

    public void Execute ()
    {
        var dt = Time.deltaTime;
        foreach (var gameEntity in m_Group.GetEntities ())
        {
            var newTime = gameEntity.lifeTimeComp.time - dt;
            if (newTime < 0)
            {
                gameEntity.isDestoryComp = true;
            }
            else
            {
                gameEntity.ReplaceLifeTimeComp (newTime);
            }
        }
    }
}